package com.cxl.springboot.controller;

import com.cxl.springboot.dao.UserCount;
import com.cxl.springboot.pojo.User;
import com.cxl.springboot.service.IUserService;
import com.cxl.springboot.util.result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@CrossOrigin
@RequestMapping("/user")
@RestController
@Api(tags = "chnexinliang的后台管理系统")
public class UserController {

    @Autowired
    IUserService UserService;
    @Autowired
    UserCount userCount;

    @GetMapping("/findByid")
    @ApiOperation(value = "根据用户id查询用户信息", notes = "演示param参数是否接受成功")
    public result findByid(@Parameter(description = "用户编号") @RequestParam("id") Integer  id) {
        User user = UserService.findById(id);
        result result = new result(user, 1);
        return result;
    }

    @GetMapping("/findByPage")
    @ApiOperation(value = "分页获取用户信息",notes = "演示分页获取用户信息")
    public ResponseEntity<result> findByPage( @Parameter(description = "页码") @RequestParam("pageNum") Integer pageNum,
                                              @Parameter(description = "页显示数量") @RequestParam("pageSize") Integer pageSize,
                                              @Parameter(description = "关键字") String key) {
        HashMap<String, Object> map = new HashMap<>();
        System.out.println("获取数据"+"第几页："+pageNum+"多少条"+pageSize);
        map.put("pageNum", pageNum*pageSize);
        map.put("pageSize", pageSize);
        String search = "";
        if(key instanceof String){
            search = key;
        }
        map.put("key", search);
        System.out.println("查询条件"+map.toString());
        List<User> userList = UserService.findUserByPage(map);
        int count = userCount.userCount(key);
        System.out.println("查询到的用户"+userList.toString());
        result result = new result(userList, 1);// 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Count", String.valueOf(count));
        headers.add("pageCount", String.valueOf((count/pageSize)+1));
        headers.add("Date", String.valueOf(new Date()));
        headers.add("Access-Control-Expose-Headers", "*");
        return new ResponseEntity<>(result,headers, HttpStatus.OK);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加用户接口",notes = "演示添加用户接口")
    public result addUser(User user) {
        user.setCreateTime(new Date());
        int i = UserService.addUser(user);
        result result = new result(i, i);
        return result;
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除用户接口",notes = "演示删除用户接口")
    public result delUser(@RequestParam("id") @Parameter(description = "用户编号")  Integer id) {
        int i = UserService.delUser(id);
        result result = new result(i, i);
        return result;
    }

    @PostMapping("/update")
    public result updateUser(User user) {
        System.out.println(user.toString());
        user.setCreateTime(new Date());
        int i = UserService.updateUser(user);
        result result = new result(i, i);
        return result;
    }

    // 制作假数据
    @GetMapping("/addTest")
    public void addTest() {
        for (int i = 0; i < 100; i++) {
            String uuid = UUID.randomUUID().toString();
            String fromUUID = getRandomNumberFromUUID(uuid, 10);
            User user = new User();
            user.setUsername("cxl--"+fromUUID);
            user.setAddress("广东--"+fromUUID);
            user.setNickname("测试--"+fromUUID);
            user.setEmail("email--"+fromUUID);
            user.setCreateTime(new Date());
            user.setPassword("123456");
            user.setPhone(generateRandomNumber(11));
            UserService.addUser(user);
        }
    }

    // 截取uuid
    public String getRandomNumberFromUUID(String uuid,int n){
        return uuid.substring(0, n);
    }
    // 截取random
    private static String generateRandomNumber(int length) {
        // 使用Random类生成随机数
        Random random = new Random();

        // 生成随机数并转换为字符串
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int digit = random.nextInt(10);  // 生成0到9之间的随机数
            stringBuilder.append(digit);
        }

        return stringBuilder.toString();
    }
}

